package com.designpatterns.singleton.lazy;

/**
 * @author Tinu 1810989@qq.com
 */
//懒汉式单例
//静态内部类实现  兼顾饿汉式的内存浪费，也兼顾synchronized性能问题
public class LazyInnerClassSingleton {

    private LazyInnerClassSingleton(){
        if(LazyHolder.LAZY != null){
            throw new RuntimeException("不允许创建多个实例");
        }
    }

    public static final LazyInnerClassSingleton getInstance() {
        //在返回结果以前，一定会先加载内部类
        return LazyHolder.LAZY;
    }

    //默认不加载
    private static class LazyHolder {
        private static final LazyInnerClassSingleton LAZY = new LazyInnerClassSingleton();
    }
}
